{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test notebook Meteorites"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Standard Library Imports\n",
    "from pathlib import Path\n",
    "\n",
    "# Installed packages\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import requests\n",
    "\n",
    "# Testing\n",
    "from IPython.utils.capture import capture_output\n",
    "from IPython.display import display\n",
    "\n",
    "# Our package\n",
    "import pandas_profiling\n",
    "from pandas_profiling.utils.cache import cache_file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_name = cache_file(\n",
    "    \"meteorites.csv\",\n",
    "    \"https://data.nasa.gov/api/views/gh4g-9sfh/rows.csv?accessType=DOWNLOAD\",\n",
    ")\n",
    "    \n",
    "df = pd.read_csv(file_name)\n",
    "    \n",
    "# Note: Pandas does not support dates before 1880, so we ignore these for this analysis\n",
    "df['year'] = pd.to_datetime(df['year'], errors='coerce')\n",
    "\n",
    "# Example: Constant variable\n",
    "df['source'] = \"NASA\"\n",
    "\n",
    "# Example: Boolean variable\n",
    "df['boolean'] = np.random.choice([True, False], df.shape[0])\n",
    "\n",
    "# Example: Mixed with base types\n",
    "df['mixed'] = np.random.choice([1, \"A\"], df.shape[0])\n",
    "\n",
    "# Example: Highly correlated variables\n",
    "df['reclat_city'] = df['reclat'] + np.random.normal(scale=5, size=(len(df)))\n",
    "\n",
    "# Example: Duplicate observations\n",
    "duplicates_to_add = pd.DataFrame(df.iloc[0:10])\n",
    "duplicates_to_add[u'name'] = duplicates_to_add[u'name'] + \" copy\"\n",
    "\n",
    "df = df.append(duplicates_to_add, ignore_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Inline report without saving\n",
    "with capture_output() as out:\n",
    "    pr = df.profile_report(sort='None', html={'style':{'full_width': True}}, progress_bar=False, minimal=True)\n",
    "    display(pr)\n",
    "\n",
    "assert len(out.outputs) == 2\n",
    "assert out.outputs[0].data['text/plain'] == '<IPython.core.display.HTML object>'\n",
    "assert all(s in out.outputs[0].data['text/html'] for s in ['<iframe', 'Profile report generated with the `pandas-profiling`'])\n",
    "assert out.outputs[1].data['text/plain'] == ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# There should also 2 progress bars in minimal mode\n",
    "with capture_output() as out:\n",
    "    pfr = df.profile_report(html={'style': {'full_width': True}}, minimal=True, progress_bar=True,lazy=False)\n",
    "\n",
    "assert all(\"FloatProgress\" in s.data['text/plain'] for s in out.outputs)\n",
    "assert len(out.outputs) == 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write to a file\n",
    "with capture_output() as out:\n",
    "    pfr.to_file(\"/tmp/example.html\")\n",
    "\n",
    "assert all(\"FloatProgress\" in s.data['text/plain'] for s in out.outputs)\n",
    "assert len(out.outputs) == 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# Print existing ProfileReport object inline\n",
    "with capture_output() as out:\n",
    "    display(pfr)\n",
    "    \n",
    "assert len(out.outputs) == 2\n",
    "assert out.outputs[0].data['text/plain'] == '<IPython.core.display.HTML object>'\n",
    "assert all(s in out.outputs[0].data['text/html'] for s in ['<iframe', 'Profile report generated with the `pandas-profiling`'])\n",
    "assert out.outputs[1].data['text/plain'] == ''"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
